﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using System.IO;
namespace syc.Sale
{
    public partial class 油管管材到货登记 : System.Web.UI.Page
    {
        protected HSSFWorkbook hssfworkbook;
        protected void Page_Load(object sender, EventArgs e)
        {
            int BillID = 0;
            if (!this.IsPostBack)
            {
                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out BillID);
                }
                this.DanHao.Text = DateTime.Now.ToString("yyyyMMddhhmmss");
                if (BillID > 0)
                {
                    SqlConnection Conn = new SqlConnection();
                    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                    Conn.Open();
                    SqlCommand Cmd = new SqlCommand();
                    Cmd.Connection = Conn;
                    Cmd.CommandText = @"SELECT [原料供货单位ID]
                                        ,[单据号]
                                        ,[车号]
                                        ,[到货日期]      
                                         FROM [SCM].[dbo].[管材到货登记] where [ID]=" + BillID;
                    Cmd.CommandType = CommandType.Text;
                    SqlDataReader Reader = Cmd.ExecuteReader();
                    if (Reader.Read())
                    {
                        this.GongHuoDanWei.DataBind();


                        this.ViewState["BillID"] = BillID;
                        this.GongHuoDanWei.Text = Reader.GetInt32(0).ToString();
                        this.DanHao.Text = Reader.GetString(1).Trim();
                        this.DanHao.ReadOnly = true;
                        if (Reader.IsDBNull(2))
                        {
                            this.CheHao.Text = "";
                        }
                        else
                        {
                            this.CheHao.Text = Reader.GetString(2).ToString();
                        }
                        this.RiQi.Text = Reader.GetDateTime(3).ToString("yyyy年M月d日");

                        Reader.Close();
                        Cmd.CommandText = @"select 管材到货明细.[原料ID],[原料].[品名] as [名称],[原料].[规格],
                                          管材到货明细.炉号,管材到货明细.批次,管材到货明细.重量,管材到货明细.磅重,管材到货明细.支数
                                          ,管材到货明细.区域,管材到货明细.生产单位ID,管材到货明细.备注 from 管材到货明细,[原料]
                                          where 管材到货明细.原料ID=[原料].ID and 管材到货明细.管材到货登记ID=" + BillID;
                        Reader = Cmd.ExecuteReader();
                        for (int i = 1; i <= 15; i++)
                        {
                            if (!Reader.Read())
                            {
                                break;
                            }
                            this.ViewState["id" + i] = Reader.GetInt32(0).ToString();
                            ((TextBox)this.FindControl("R" + 1 + "_C1")).Text = Reader.GetString(1);
                            ((TextBox)this.FindControl("R" + 1 + "_C2")).Text = Reader.GetString(2);
                            if (Reader.IsDBNull(3))
                            {
                                ((TextBox)this.FindControl("R" + i + "_C3")).Text = "";
                            }
                            else
                            {
                                ((TextBox)this.FindControl("R" + i + "_C3")).Text = Reader.GetString(3);
                            }
                            ((TextBox)this.FindControl("R" + i + "_C3")).ReadOnly = false;
                            ((TextBox)this.FindControl("R" + i + "_C3")).CssClass = "textbox";
                            if (Reader.IsDBNull(4))
                            {
                                ((TextBox)this.FindControl("R" + i + "_C4")).Text = "";
                            }
                            else
                            {
                                ((TextBox)this.FindControl("R" + i + "_C4")).Text = Reader.GetString(4);
                            }
                            ((TextBox)this.FindControl("R" + i + "_C4")).ReadOnly = false;
                            ((TextBox)this.FindControl("R" + i + "_C4")).CssClass = "textbox";
                            if (Reader.IsDBNull(5))
                            {
                                ((TextBox)this.FindControl("R" + i + "_C5")).Text = "";
                            }
                            else
                            {
                                ((TextBox)this.FindControl("R" + i + "_C5")).Text = Reader.GetDouble(5).ToString();
                            }
                            ((TextBox)this.FindControl("R" + i + "_C5")).ReadOnly = false;
                            ((TextBox)this.FindControl("R" + i + "_C5")).CssClass = "textbox";
                            if (Reader.IsDBNull(6))
                            {
                                ((TextBox)this.FindControl("R" + i + "_C6")).Text = "";
                            }
                            else
                            {
                                ((TextBox)this.FindControl("R" + i + "_C6")).Text = Reader.GetDouble(6).ToString();
                            }
                            ((TextBox)this.FindControl("R" + i + "_C6")).ReadOnly = false;
                            ((TextBox)this.FindControl("R" + i + "_C6")).CssClass = "textbox";
                            if (Reader.IsDBNull(7))
                            {
                                ((TextBox)this.FindControl("R" + i + "_C7")).Text = "";
                            }
                            else
                            {
                                ((TextBox)this.FindControl("R" + i + "_C7")).Text = Reader.GetInt32(7).ToString();
                            }
                            ((TextBox)this.FindControl("R" + i + "_C7")).ReadOnly = false;
                            ((TextBox)this.FindControl("R" + i + "_C7")).CssClass = "textbox";
                            if (Reader.IsDBNull(8))
                            {
                                ((TextBox)this.FindControl("R" + i + "_C8")).Text = "";
                            }
                            else
                            {
                                ((TextBox)this.FindControl("R" + i + "_C8")).Text = Reader.GetString(8);
                            }
                            ((TextBox)this.FindControl("R" + i + "_C8")).ReadOnly = false;
                            ((TextBox)this.FindControl("R" + i + "_C8")).CssClass = "textbox";
                            //((DropDownList)this.FindControl("DropDownList" + i)).SelectedValue = Reader.GetInt32(9).ToString();
                            //((DropDownList)this.FindControl("DropDownList" + i)).Visible = true;
                            ((TextBox)this.FindControl("R" + i + "_C10")).Text = Reader.GetString(10);
                            ((TextBox)this.FindControl("R" + i + "_C10")).ReadOnly = false;
                            ((TextBox)this.FindControl("R" + i + "_C10")).CssClass = "textbox";
                            Button_Calc_Click(null, null);

                            //((Button)this.FindControl("Button_Add" + i)).Text = "删 除";
                        }
                        Reader.Close();
                    }
                    else
                    {
                        Reader.Close();
                    }

                    Conn.Close();
                    Reader.Dispose();
                    Cmd.Dispose();
                    Conn.Dispose();
                }
                else
                {
                    this.RiQi.Text = DateTime.Today.ToString("yyyy年M月d日");
                }
            }
        }

        protected void Button_New_Click(object sender, EventArgs e)
        {
            Response.Redirect("YuanLiao_DaoHuo_Register.aspx");
        }

        protected void Button_Finish_Click(object sender, EventArgs e)
        {
            Button_Calc_Click(null, null);
            if (this.CheHao.Text == "")
            {
                this.Message.Text = "·请输入车号";
                return;
            }
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlTransaction Tran = Conn.BeginTransaction();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.Transaction = Tran;
            try
            {
                Cmd.CommandText = "GuanCai_DaoHuo";
                Cmd.CommandType = CommandType.StoredProcedure;
                if (this.ViewState["BillID"] == null)
                {
                    this.ViewState["BillID"] = -1;
                }
                Cmd.Parameters.Add(new SqlParameter("@原料供货单位ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.GongHuoDanWei.Text));
                Cmd.Parameters.Add(new SqlParameter("@单据号", SqlDbType.NChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.DanHao.Text));
                Cmd.Parameters.Add(new SqlParameter("@车号", SqlDbType.NChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.CheHao.Text));
                Cmd.Parameters.Add(new SqlParameter("@到货日期", SqlDbType.Date, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, DateTime.Parse(this.RiQi.Text)));
                Cmd.Parameters.Add(new SqlParameter("@重量", SqlDbType.Float, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, float.Parse(this.R16_C5.Text)));
                Cmd.Parameters.Add(new SqlParameter("@磅重", SqlDbType.Float, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, float.Parse(this.R16_C6.Text)));
                Cmd.Parameters.Add(new SqlParameter("@支数", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, int.Parse(this.R16_C7.Text)));
                Cmd.Parameters.Add(new SqlParameter("@BillID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.ViewState["BillID"]));
                Cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 4, ParameterDirection.Output, true, 0, 0, "", DataRowVersion.Proposed, null));
                Cmd.ExecuteNonQuery();
                this.ViewState["BillID"] = (int)Cmd.Parameters["@ID"].Value;
                int BillID = (int)this.ViewState["BillID"];
                if (BillID > 0)
                {
                    Cmd.CommandText = "GuanCai_DaoHuo_MingXi";
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.Clear();
                    Cmd.Parameters.Add(new SqlParameter("@管材到货登记ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, BillID));
                    Cmd.Parameters.Add(new SqlParameter("@原料ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, 0));
                    Cmd.Parameters.Add(new SqlParameter("@炉号", SqlDbType.NVarChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, null));
                    Cmd.Parameters.Add(new SqlParameter("@批次", SqlDbType.NVarChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, null));
                    Cmd.Parameters.Add(new SqlParameter("@重量", SqlDbType.Float, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, 0));
                    Cmd.Parameters.Add(new SqlParameter("@磅重", SqlDbType.Float, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, 0));
                    Cmd.Parameters.Add(new SqlParameter("@支数", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, 0));
                    Cmd.Parameters.Add(new SqlParameter("@区域", SqlDbType.Char, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, null));
                    //  Cmd.Parameters.Add(new SqlParameter("@使用单位", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, 0));
                    Cmd.Parameters.Add(new SqlParameter("@备注", SqlDbType.NVarChar, 150, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, null));
                    for (int i = 1; i <= 15; i++)
                    {
                        if ((string)((TextBox)this.FindControl("R" + i + "_C3")).Text != "")
                        {
                            Cmd.Parameters["@原料ID"].Value = this.GuanCai.SelectedValue;
                            Cmd.Parameters["@炉号"].Value = ((TextBox)this.FindControl("R" + i + "_C3")).Text;
                            Cmd.Parameters["@批次"].Value = ((TextBox)this.FindControl("R" + i + "_C4")).Text;
                            Cmd.Parameters["@重量"].Value = float.Parse(((TextBox)this.FindControl("R" + i + "_C5")).Text);
                            Cmd.Parameters["@磅重"].Value = float.Parse(((TextBox)this.FindControl("R" + i + "_C6")).Text);
                            Cmd.Parameters["@支数"].Value = int.Parse(((TextBox)this.FindControl("R" + i + "_C7")).Text);
                            Cmd.Parameters["@区域"].Value = ((TextBox)this.FindControl("R" + i + "_C8")).Text;
                            if (((TextBox)this.FindControl("R" + i + "_C10")).Text == "一分厂" || ((TextBox)this.FindControl("R" + i + "_C10")).Text == "1分厂" || ((TextBox)this.FindControl("R" + i + "_C10")).Text == "1")
                            {
                                Cmd.Parameters["@备注"].Value = "1";
                            }
                            if (((TextBox)this.FindControl("R" + i + "_C10")).Text == "二分厂" || ((TextBox)this.FindControl("R" + i + "_C10")).Text == "2分厂" || ((TextBox)this.FindControl("R" + i + "_C10")).Text == "2")
                            {
                                Cmd.Parameters["@备注"].Value = "2";
                            }
                            if (((TextBox)this.FindControl("R" + i + "_C10")).Text == "三分厂" || ((TextBox)this.FindControl("R" + i + "_C10")).Text == "3分厂" || ((TextBox)this.FindControl("R" + i + "_C10")).Text == "3")
                            {
                                Cmd.Parameters["@备注"].Value = "3";
                            }
                            if (((TextBox)this.FindControl("R" + i + "_C10")).Text == "四分厂" || ((TextBox)this.FindControl("R" + i + "_C10")).Text == "4分厂" || ((TextBox)this.FindControl("R" + i + "_C10")).Text == "4")
                            {
                                Cmd.Parameters["@备注"].Value = "4";
                            }
                            if (((TextBox)this.FindControl("R" + i + "_C10")).Text == "五分厂" || ((TextBox)this.FindControl("R" + i + "_C10")).Text == "5分厂" || ((TextBox)this.FindControl("R" + i + "_C10")).Text == "5")
                            {
                                Cmd.Parameters["@备注"].Value = "5";
                            }
                            if (((TextBox)this.FindControl("R" + i + "_C10")).Text == "" || ((TextBox)this.FindControl("R" + i + "_C10")).Text == null)
                            {
                                Cmd.Parameters["@备注"].Value = "";
                            }
                            Cmd.ExecuteNonQuery();
                        }
                    }
                    this.Message.Text += "·保存成功";
                    this.DanHao.ReadOnly = true;
                }
                else
                {
                    this.Message.Text = "·单价编号(No.)已经存在";
                }

                Tran.Commit();
            }
            catch (Exception err)
            {
                Tran.Rollback();
                this.Message.Text = ".请正确填写使用分厂！";
            }
            Cmd.Dispose();
            Tran.Dispose();
            Conn.Close();
            Conn.Dispose();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("YuanLiao_DaoHuo_Search.aspx?GuanCai="+Request.QueryString["GuanCai"]);
        }

        //protected void Button_Add_Command(object sender, CommandEventArgs e)
        //{
        //    int row;
        //    int.TryParse(e.CommandArgument.ToString(), out row);
        //    this.ViewState["row"] = row;

        //    if (this.ViewState["id" + row] == null)
        //    {
        //        this.Panel_Grid.Visible = true;
        //        this.Message.Text = "·准备添加";
        //    }
        //    else
        //    {
        //        this.ViewState["id" + row] = null;
        //        ((TextBox)this.FindControl("R" + row + "_C1")).Text = "";
        //        ((TextBox)this.FindControl("R" + row + "_C2")).Text = "";
        //        ((TextBox)this.FindControl("R" + row + "_C3")).Text = "";
        //        ((TextBox)this.FindControl("R" + row + "_C3")).ReadOnly = true;
        //        ((TextBox)this.FindControl("R" + row + "_C3")).CssClass = "blank_center";
        //        ((TextBox)this.FindControl("R" + row + "_C4")).Text = "";
        //        ((TextBox)this.FindControl("R" + row + "_C4")).ReadOnly = true;
        //        ((TextBox)this.FindControl("R" + row + "_C4")).CssClass = "blank_center";
        //        ((TextBox)this.FindControl("R" + row + "_C5")).ReadOnly = true;
        //        ((TextBox)this.FindControl("R" + row + "_C5")).CssClass = "blank_center";
        //        ((TextBox)this.FindControl("R" + row + "_C5")).Text = "";
        //        ((TextBox)this.FindControl("R" + row + "_C6")).ReadOnly = true;
        //        ((TextBox)this.FindControl("R" + row + "_C6")).CssClass = "blank_center";
        //        ((TextBox)this.FindControl("R" + row + "_C6")).Text = "";
        //        ((TextBox)this.FindControl("R" + row + "_C7")).CssClass = "blank_center";
        //        ((TextBox)this.FindControl("R" + row + "_C7")).Text = "";
        //        ((TextBox)this.FindControl("R" + row + "_C7")).ReadOnly = true;
        //        ((TextBox)this.FindControl("R" + row + "_C8")).CssClass = "blank_center";
        //        ((TextBox)this.FindControl("R" + row + "_C8")).Text = "";
        //        ((TextBox)this.FindControl("R" + row + "_C8")).ReadOnly = true;
        //        ((DropDownList)this.FindControl("DropDownList" + row)).Visible = false;
        //        ((TextBox)this.FindControl("R" + row + "_C10")).CssClass = "blank_center";
        //        ((TextBox)this.FindControl("R" + row + "_C10")).Text = "";
        //        ((TextBox)this.FindControl("R" + row + "_C10")).ReadOnly = true;


        //        ((Button)this.FindControl("Button_Add" + row)).Text = "添 加";
        //        this.Message.Text = "·删除成功";
        //    }
        //}

        protected void ImageButton_Cancel_Click(object sender, ImageClickEventArgs e)
        {
            this.Panel_Grid.Visible = false;
            this.Message.Text = "·已取消添加";
        }

        protected void Button1_Command(object sender, CommandEventArgs e)
        {

            //    int row = (int)this.ViewState["row"];
            //    string[] Args = e.CommandArgument.ToString().Split("\t".ToCharArray());

            //    this.ViewState["id" + row] = Args[0];
            //    ((TextBox)this.FindControl("R" + row + "_C1")).Text = "套管光管";
            //    ((TextBox)this.FindControl("R" + row + "_C2")).Text = "";
            //    ((TextBox)this.FindControl("R" + row + "_C3")).Text = "";
            //    ((TextBox)this.FindControl("R" + row + "_C3")).ReadOnly = false;
            //    ((TextBox)this.FindControl("R" + row + "_C3")).CssClass = "textbox";
            //    ((TextBox)this.FindControl("R" + row + "_C4")).Text = "";
            //    ((TextBox)this.FindControl("R" + row + "_C4")).ReadOnly = false;
            //    ((TextBox)this.FindControl("R" + row + "_C4")).CssClass = "textbox";
            //    ((TextBox)this.FindControl("R" + row + "_C5")).Text = "";
            //    ((TextBox)this.FindControl("R" + row + "_C5")).ReadOnly = false;
            //    ((TextBox)this.FindControl("R" + row + "_C5")).CssClass = "textbox";
            //    ((TextBox)this.FindControl("R" + row + "_C6")).Text = "";
            //    ((TextBox)this.FindControl("R" + row + "_C6")).ReadOnly = false;
            //    ((TextBox)this.FindControl("R" + row + "_C6")).CssClass = "textbox";
            //    ((TextBox)this.FindControl("R" + row + "_C7")).Text = "";
            //    ((TextBox)this.FindControl("R" + row + "_C7")).ReadOnly = false;
            //    ((TextBox)this.FindControl("R" + row + "_C7")).CssClass = "textbox";
            //    ((TextBox)this.FindControl("R" + row + "_C8")).Text = "";
            //    ((TextBox)this.FindControl("R" + row + "_C8")).ReadOnly = false;
            //    ((TextBox)this.FindControl("R" + row + "_C8")).CssClass = "textbox";
            //    ((DropDownList)this.FindControl("DropDownList" + row)).Visible = true;
            //    ((TextBox)this.FindControl("R" + row + "_C10")).Text = "";
            //    ((TextBox)this.FindControl("R" + row + "_C10")).ReadOnly = false;
            //    ((TextBox)this.FindControl("R" + row + "_C10")).CssClass = "textbox";

            //    ((Button)this.FindControl("Button_Add" + row)).Text = "删 除";
            //    this.Panel_Grid.Visible = false;
            //    this.Message.Text = "·添加成功";
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }

        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }
        }

        protected void Button_Calc_Click(object sender, EventArgs e)
        {
            float f0 = 0, f1 = 0, sum1 = 0, sum2 = 0;
            int f2 = 0, sum3 = 0;
            this.Message.Text = "";
            for (int i = 1; i <= 15; i++)
            {
                if ((string)((TextBox)this.FindControl("R" + i + "_C3")).Text != "")
                {
                    float.TryParse(((TextBox)this.FindControl("R" + i + "_C5")).Text, out f0);
                    float.TryParse(((TextBox)this.FindControl("R" + i + "_C6")).Text, out f1);
                    int.TryParse(((TextBox)this.FindControl("R" + i + "_C7")).Text, out f2);
                    if (f0 < 0 || f1 < 0 || f2 < 0)
                    {
                        this.Message.Text += " ·数量或者单价输入有误";
                    }
                    else
                    {
                        sum1 += f0;
                        sum2 += f1;
                        sum3 += f2;
                    }
                }
            }
            ((TextBox)this.FindControl("R16" + "_C5")).Text = sum1.ToString();
            ((TextBox)this.FindControl("R16" + "_C6")).Text = sum2.ToString();
            ((TextBox)this.FindControl("R16" + "_C7")).Text = sum3.ToString();
            if (this.Message.Text == "")
            {
                this.Message.Text = "·完成金额计算";
            }
        }

        protected void WriteToFile()
        {
            DateTime d;
            if (!DateTime.TryParse(this.RiQi.Text, out d))
            {
                d = DateTime.Today;
            }
            Response.ContentType = "application/ms-excel";
            Response.AddHeader("content-disposition", "attachment; filename=\"" + d.ToShortDateString() + ".xls\"");
            hssfworkbook.Write(Response.OutputStream);
        }

        protected void InitializeWorkbook()
        {

            FileStream file = new FileStream(HttpContext.Current.Server.MapPath("/Sale/模板/管材到货登记.xls"), FileMode.Open, FileAccess.Read);

            hssfworkbook = new HSSFWorkbook(file);

            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "管材到货登记";
            hssfworkbook.DocumentSummaryInformation = dsi;

            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Subject = "报表";
            si.Title = "管材到货登记";
            hssfworkbook.SummaryInformation = si;
        }
        protected void Button_To_Click(object sender, EventArgs e)
        {
            InitializeWorkbook();
            HSSFSheet sheet1 = hssfworkbook.GetSheet("管材到货登记");
            sheet1.GetRow(1).GetCell(1).SetCellValue(this.GongHuoDanWei.SelectedItem.Text);
            sheet1.GetRow(1).GetCell(3).SetCellValue(this.RiQi.Text);
            sheet1.GetRow(1).GetCell(5).SetCellValue(this.CheHao.Text.Trim());
            sheet1.GetRow(1).GetCell(7).SetCellValue(this.DanHao.Text);
            for (int i = 1; i <= 15; i++)
            {
                if ((string)((TextBox)this.FindControl("R" + i + "_C3")).Text != "")
                {
                    sheet1.GetRow(2 + i).GetCell(0).SetCellValue(((TextBox)this.FindControl("R" + 1 + "_C1")).Text);
                    sheet1.GetRow(2 + i).GetCell(1).SetCellValue(((TextBox)this.FindControl("R" + 1 + "_C2")).Text);
                    sheet1.GetRow(2 + i).GetCell(2).SetCellValue(((TextBox)this.FindControl("R" + i + "_C3")).Text);
                    sheet1.GetRow(2 + i).GetCell(3).SetCellValue(((TextBox)this.FindControl("R" + i + "_C4")).Text);
                    sheet1.GetRow(2 + i).GetCell(4).SetCellValue(float.Parse(((TextBox)this.FindControl("R" + i + "_C5")).Text));
                    sheet1.GetRow(2 + i).GetCell(5).SetCellValue(float.Parse(((TextBox)this.FindControl("R" + i + "_C6")).Text));
                    sheet1.GetRow(2 + i).GetCell(6).SetCellValue(int.Parse(((TextBox)this.FindControl("R" + i + "_C7")).Text));
                    sheet1.GetRow(2 + i).GetCell(7).SetCellValue(((TextBox)this.FindControl("R" + i + "_C8")).Text);
                    if (((TextBox)this.FindControl("R" + i + "_C10")).Text == "1")
                    {
                        sheet1.GetRow(2 + i).GetCell(8).SetCellValue("一分厂");
                    }
                    if (((TextBox)this.FindControl("R" + i + "_C10")).Text == "2")
                    {
                        sheet1.GetRow(2 + i).GetCell(8).SetCellValue("二分厂");
                    }
                    if (((TextBox)this.FindControl("R" + i + "_C10")).Text == "3")
                    {
                        sheet1.GetRow(2 + i).GetCell(8).SetCellValue("三分厂");
                    }
                    if (((TextBox)this.FindControl("R" + i + "_C10")).Text == "4")
                    {
                        sheet1.GetRow(2 + i).GetCell(8).SetCellValue("四分厂");
                    }
                    if (((TextBox)this.FindControl("R" + i + "_C10")).Text == "5")
                    {
                        sheet1.GetRow(2 + i).GetCell(8).SetCellValue("五分厂");
                    }
                    if (((TextBox)this.FindControl("R" + i + "_C10")).Text == "" || ((TextBox)this.FindControl("R" + i + "_C10")).Text == null)
                    {
                        sheet1.GetRow(2 + i).GetCell(8).SetCellValue("");
                    }
                }
            }
            sheet1.GetRow(18).GetCell(4).SetCellValue(((TextBox)this.FindControl("R16_C5")).Text);
            sheet1.GetRow(18).GetCell(5).SetCellValue(((TextBox)this.FindControl("R16_C6")).Text);
            sheet1.GetRow(18).GetCell(6).SetCellValue(((TextBox)this.FindControl("R16_C7")).Text);  
            WriteToFile();
            Response.End();
        }            
    }
}
